package com.huiminpay.transaction.pay;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.huiminpay.common.cache.util.EncryptUtil;
import com.huiminpay.transaction.api.dto.PayOrderDTO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;


import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class AlipayController {

    @GetMapping("alipay/index")
    public void index(HttpServletResponse response) throws AlipayApiException, IOException {
     //
        String geteway = "https://openapi.alipaydev.com/gateway.do";
        String appId= "2021000117676279";
        String privatekey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCb7ohb7Loeo1bKcyChWovnIUb43sxYkbH2uAnan/BhpJmJEaVcyNCK9YOM+vM+SJ1ZvLaNqqfOQ9/hkQKQXMiGAt3n8DCvRE54t4B2HKvar7sXFi0NtxPiH7StkPiTdzgm+X6yTBcPnB7whc30cqYMAa3szxCyRE/0nzaCA5tRnZC/Lak2n4Omgb4PEiMxY9v2J72Qb6zaiwYbISdKKddCocE+7CU5R8rQGeL1GFOx5D+7cqOF52+jBAd6u8O7Yz0hXlwyLzs4xEyhJV2OPuk/6NeY92NHxde0Cmb4x9CQKHiMjVP5R7+lj0MUlaz8GddPrhqI2nblQvZ0huYkJkEHAgMBAAECggEBAJoY6szdbzlj9htwFukeUHADpN3BYHSO1y6YxbQ2pxn3znt04tSE6ALLikvCnKwKmuXZ5xatddQTo6aI5d3As7IsFxxlc9dhS6NkxSD0SKl/TQcNtUx+hrnK14VHZUBmaympGpodNBULFlBgCx7uRkZLSpRiNX/WnqWCyh1GbzIBVea3tpIcGIKpJXK73Bgm2zKjA4dCoHnn6MtIZzzrMjDIhBNipgOhh/b2LczYxaTqM7gvhrJqCoTgoEKi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jCj/uwFHjCQYABZkzv8eBOwp+h/IkJreypdKB+vATjmxdyMiU0qD0+jyPVqbjlAs0p+2R2ik9iat45iW/W+MSHpyZatIigLeRLeeSG95dKXcehKwh1419Ay2y4Kn/FWDYQTuRjRBHG/IL4PWSZMTBjovFHHPKO9pmxisQmDpcVxR3s5ZIErNK3+MaVwy+QTo+fWLejiVv91k/IauIVU3zyYREUuhNm2k7M1K4LLGJ1g2bFZfAjT49XiSpJWL43WdCkP9MobmRL7Cabp25Ery5vr63yHLeEPvl5zYteNPjjgvQIDAQAB";
        String charset = "utf-8";//编解码需要一致
        AlipayClient alipayClient = new DefaultAlipayClient(geteway,appId,privatekey,"json",charset,alipaypublickey,"RSA2");
        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        String out_trade_no= System.currentTimeMillis()+"";
        request.setBizContent("{" +
                " \"out_trade_no\":\"20150320010101032\"," +
                " \"total_amount\":\"1000\"," +
                " \"subject\":\"Iphone6 160G\"," +
                " \"product_code\":\"QUICK_WAP_PAY\"" +
                " }");
        //{"subject":"HUAWEI100p","out_trade_no":"10000","total_amount":"9999999","quit_url":"","product_code":"QUICK_WAP_PAY"}
        AlipayTradeWapPayResponse aliresponse = alipayClient.pageExecute(request);
        if(aliresponse.isSuccess()){
            System.out.println("调用成功");
            String body = aliresponse.getBody();
            response.setContentType("text/html; charset="+charset);
            response.getWriter().write(body);
            response.getWriter().flush();
            response.getWriter().close();

        } else {
            System.out.println("调用失败");
        }

    }
    @GetMapping("alipay/notify")
    public void notifyUrl(String out_trade_no){
        //更新数据库

    }
    @GetMapping("qr/{ticket}")
    public void index(@PathVariable String ticket){
       //非空校验
        //解码
        String s = EncryptUtil.decodeUTF8StringBase64(ticket);
        JSON.parseObject(s, PayOrderDTO.class);

    }
}
